//
//  ProductTableView.m
//  SZBank
//
//  Created by LuoYao on 16/7/26.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "ProductTableView.h"


@interface ProductTableView ()<UIScrollViewDelegate>
{
    CGFloat cwitdh,ckheight;
    NSArray *dataArray;/**< 表格数据源 */
}
@property (nonatomic) UIScrollView *scrollView;/**< 滑动视图 */

@end
@implementation ProductTableView

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        
        cwitdh = frame.size.width;
        ckheight = frame.size.height;
        dataArray = array;
        
        
        for (int i = 0; i < dataArray.count; i ++) {
            //行
            
            //计算高度
            CGSize size = [NSString textString:dataArray[i][@"detail"] fontSize:FONT_SIZE maxWidth:cwitdh * 0.6];

            //横线
            UILabel *lineH = [[UILabel alloc] initWithFrame:CGRectMake(0, !self.scrollView.subviews.lastObject ? 0 : CGRectGetMaxY(self.scrollView.subviews.lastObject.frame), cwitdh, 2)];
            lineH.backgroundColor = [UIColor grayColor];
            [self.scrollView addSubview:lineH];
            
            for (int j = 0; j < 2; j ++) {
                //列
                //竖线
                UILabel *lineV = [[UILabel alloc] initWithFrame:CGRectMake(2 + j * cwitdh * 0.3 - 2, CGRectGetMaxY(lineH.frame), 1, size.height + 50)];
                lineV.backgroundColor = [UIColor grayColor];
                [self.scrollView addSubview:lineV];
                
                //显示数据
                UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake((j == 0 ? cwitdh * 0.01 : cwitdh * 0.35), j == 0 ? CGRectGetMaxY(lineH.frame) : CGRectGetMaxY(lineH.frame) + 25, (j == 0 ? cwitdh * 0.3 : cwitdh * 0.6), j == 0 ? size.height + 50: size.height + 50)];
                textLabel.textAlignment = (j == 0 ? NSTextAlignmentCenter : NSTextAlignmentLeft);
                textLabel.textColor = [UIColor blackColor];
                
                textLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
                textLabel.numberOfLines = 0;
                
                textLabel.text = j == 0 ? dataArray[i][@"title"] : dataArray[i][@"detail"];
                if (j == 1) {
                    [textLabel sizeToFit];
                }
                
                [self.scrollView addSubview:textLabel];
                
                if (j == 1) {
                    //竖线(最右边)
                    UILabel *lineV = [[UILabel alloc] initWithFrame:CGRectMake(cwitdh - 2 , CGRectGetMaxY(lineH.frame), 2, size.height + 50)];
                    lineV.backgroundColor = [UIColor grayColor];
                    [self.scrollView addSubview:lineV];
                }
                
            }
            
            if (i == dataArray.count - 1) {
                //横线
                UILabel *lineH = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollView.subviews.lastObject.frame), cwitdh, 2)];
                lineH.backgroundColor = [UIColor grayColor];
                [self.scrollView addSubview:lineH];
            }
        }
        self.scrollView.contentSize = CGSizeMake(cwitdh, CGRectGetMaxY(self.scrollView.subviews.lastObject.frame) + ckheight * 0.2);
        [self addSubview:self.scrollView];
        
    }
    return self;
}


#pragma mark - getter
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.delegate = self;

        _scrollView.bounces = NO;
    }
    return _scrollView;
}
@end
